package br.elsa.survey.statistics;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.Transient;

import br.elsa.survey.dao.QuestionDao;
import br.elsa.survey.dao.SurveyDao;

/**
 * {@link ApplicationScoped} Component that holds the system statistics like
 * questions count. This component is also {@link Named}, being possible to
 * access the application instance using the component name ({@code statistics})
 * 
 * @author diego
 * @since 2010-10-22
 */
@Named
@ApplicationScoped
public class Statistics implements Serializable {

	private static final long serialVersionUID = -3170903570960612021L;

	private int checkboxQuestionsCount;
	private int radioQuestionsCount;
	private int discursiveQuestionsCount;
	private int surveysCount;
	
	@Inject
	@Transient
	private QuestionDao questionsDao;
	
	@Inject
	@Transient
	private SurveyDao surveyDao;

	/**
	 * Default constructor used by CDI.
	 */
	public Statistics() {
	}

	/**
	 * Constructor used by test classes to inject Mocked dependencies
	 * @param surveyDao 
	 * 
	 * @param questionsDao
	 *            Mocked dependency
	 */
	public Statistics(SurveyDao surveyDao, QuestionDao questionsDao) {
		this.surveyDao = surveyDao;
		this.questionsDao = questionsDao;
	}

	/**
	 * Loads the statistics data.
	 * 
	 * <p>
	 * Called after the creation of the component instance by the container
	 * </p>
	 * 
	 * @see PostConstruct
	 */
	@PostConstruct
	public void loadStatistics() {
		this.checkboxQuestionsCount = questionsDao.checkboxQuestionsCount();
		this.radioQuestionsCount = questionsDao.radioQuestionsCount();
		this.discursiveQuestionsCount = questionsDao.discursiveQuestionsCount();
		this.surveysCount = surveyDao.surveysCount();
	}

	public int getCheckboxQuestionsCount() {
		return checkboxQuestionsCount;
	}

	public int getRadioQuestionsCount() {
		return radioQuestionsCount;
	}

	public int getDiscursiveQuestionsCount() {
		return discursiveQuestionsCount;
	}

	public int getSurveysCount() {
		return surveysCount;
	}

	/**
	 * Calculates the questions count.
	 * 
	 * <p>
	 * This calculus is made by summing the counts of registered questions in
	 * the system of each type (CheckboxQuestion, RadioQuestion,
	 * DiscursiveQuestion, etc)
	 * </p>
	 * 
	 * @return the questions count
	 */
	public int getQuestionsCount() {

		return checkboxQuestionsCount + radioQuestionsCount + discursiveQuestionsCount;
	}

}
